// pages/user_center/car_company/car_manage/car_manage_add.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carname: '',
    seatcount: '',
    pinpai: '',
    xilie: '',
    chexing: '',
    color: '',
    pagetype: '确认新增',
    carid: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo)

    console.log(options)
    if (options.pagetype == 'update') {
      console.log('修改界面')
      this.setData({
        carname: options.cph,
        seatcount: options.seat,
        chexing: options.cartype,
        color: options.color,
        pagetype: '确认修改',
        carid: options.carid
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  carNameInput: function (e) {
    this.setData({
      carname: e.detail.value
    })
    console.log(this.data.carname)
  },

  seatCount: function (e) {
    this.setData({
      seatcount: e.detail.value
    })
    console.log(this.data.seatcount)
  },

  pinpai: function (e) {
    this.setData({
      pinpai: e.detail.value
    })
    console.log(this.data.pinpai)
  },

  xilie: function (e) {
    this.setData({
      xilie: e.detail.value
    })
    console.log(this.data.xilie)
  },

  chexing: function (e) {
    this.setData({
      chexing: e.detail.value
    })
    console.log(this.data.chexing)
  },

  color: function (e) {
    this.setData({
      color: e.detail.value
    })
    console.log(this.data.color)
  },

  showError: function (msg) {
    var that = this;
    that.setData({
      formError: true,
      errorMessage: msg
    });

    setTimeout(function () {
      that.setData({
        formError: false,
        errorMessage: '未知错误'
      });
    }, 2000);
  },

  confirm: function () {
    var that = this
    var userInfo = wx.getStorageSync('userInfo');
    if (!that.data.carname) {
      that.showError('请填写车牌号')
      return
    }

    if (!that.data.seatcount) {
      that.showError('请填写座位数')
      return
    }

    if (that.data.pagetype == '确认新增') {
      wx.request({
        url: 'https://www.lanshuicar.com/Server/AddCar.ashx',
        data: {
          method: 2,
          cph: that.data.carname,
          CarType: that.data.chexing,
          Color: that.data.color,
          SCount: that.data.seatcount,
          id: userInfo.ZID,
          Type: userInfo.USerType,
          Judge: 1
        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        // header: {}, // 设置请求的 header
        success: function (res) {
          // success
          console.log(res)
          var code = res.data.code;
          if (code == 0) {
            var pages = getCurrentPages()
            var prevPage = pages[pages.length - 1]  //当前界面
            var prevPage = pages[pages.length - 2]  //上一个页面
            // var that = this
            prevPage.setData({
              isadd: true
            })
            wx.navigateBack({

            })
          } else {
            console.log(res.data.msg)
            var msg = res.data.msg
            that.showError(msg)
            // that.showError(res.data.msg)
          }
        },
        fail: function () {
          // fail
        },
        complete: function () {
          // complete
        }
      })
    } else {
      wx.request({
        url: 'https://www.lanshuicar.com/Server/AddCar.ashx',
        data: {
          method: 3,
          cph: that.data.carname,
          CarType: that.data.chexing,
          Color: that.data.color,
          SCount: that.data.seatcount,
          id: userInfo.ZID,
          Type: userInfo.USerType,
          Judge: 1,
          carid: that.data.carid
        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        // header: {}, // 设置请求的 header
        success: function (res) {
          // success
          console.log(res)
          var code = res.data.code;
          if (code == 0) {
            var pages = getCurrentPages()
            var prevPage = pages[pages.length - 1]  //当前界面
            var prevPage = pages[pages.length - 2]  //上一个页面
            // var that = this
            prevPage.setData({
              isupdate: true
            })
            wx.navigateBack({

            })
          } else {
            console.log(res.data.msg)
            var msg = res.data.msg
            that.showError(msg)
            // that.showError(res.data.msg)
          }
        },
        fail: function () {
          // fail
        },
        complete: function () {
          // complete
        }
      })
    }

  }
})